﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DisplayCoverage.CApPaintDisplayCoverageViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Paint.DeleteCoverage;
using DnProcessSimulateCommands.Paint.PaintDuringSimulation;
using EngineeringInternalExtension;
using EngineeringInternalExtension.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Paint.DisplayCoverage;

public class CApPaintDisplayCoverageViewModel : INotifyPropertyChanged
{
  private List<Color> m_selectedColorList = new List<Color>();
  private List<float> m_scaleValueList = new List<float>();
  private List<string> m_scaleNameList = new List<string>();
  private string m_selectedScaleName = "ColorScale";
  private bool m_HSRFilter;
  private bool m_registeredForSimulationEvents;
  private bool m_isSimulationRunning;
  private bool m_displayCoverageMode = true;
  private bool m_displayBrush = true;
  private bool m_displayThicknessValue = true;
  private bool m_isMeshEmphasizing;
  private bool m_isMeshReady;
  private bool m_isButtonEnabled;

  public CApPaintDisplayCoverageViewModel()
  {
    this.SelectedColorList = CUiPaintDisplayCoverageUtil.LoadColorListFromRegistry();
    this.UpdateColorPaletteSingleton();
    TxObjectList<Tx2Or3DimensionalGeometry> ofDisplayedParts = CUiPaintDisplayCoverageUtil.GetMeshEntitiesOfDisplayedParts();
    this.IsMeshReady = ofDisplayedParts != null && ((Collection<Tx2Or3DimensionalGeometry>) ofDisplayedParts).Count != 0;
    if (!CApPaintDuringSimulationManager.Instance.UsePaintThickness)
      return;
    this.UpdateScaleNameList();
    this.SetInitialScaleName();
  }

  public bool IsButtonEnabled
  {
    get => this.m_isButtonEnabled;
    set
    {
      this.m_isButtonEnabled = value;
      this.OnPropertyChanged(nameof (IsButtonEnabled));
    }
  }

  public bool IsMeshEmphasizing
  {
    get => this.m_isMeshEmphasizing;
    set
    {
      this.m_isMeshEmphasizing = value;
      CApPaintDeleteCoverageLogic.Instance.IsMeshEmphasizing = value;
      this.ValidateIsButtonEnabled();
      this.OnPropertyChanged(nameof (IsMeshEmphasizing));
    }
  }

  public bool IsMeshReady
  {
    get => this.m_isMeshReady;
    set
    {
      this.m_isMeshReady = value;
      this.ValidateIsButtonEnabled();
      this.OnPropertyChanged(nameof (IsMeshReady));
    }
  }

  public bool DisplayCoverageMode
  {
    get => this.m_displayCoverageMode;
    set
    {
      this.m_displayCoverageMode = value;
      this.OnPropertyChanged(nameof (DisplayCoverageMode));
    }
  }

  public bool DisplayBrush
  {
    get => this.m_displayBrush;
    set
    {
      this.m_displayBrush = value;
      this.OnPropertyChanged(nameof (DisplayBrush));
    }
  }

  public bool DisplayThicknessValue
  {
    get => this.m_displayThicknessValue;
    set
    {
      this.m_displayThicknessValue = value;
      this.OnPropertyChanged(nameof (DisplayThicknessValue));
    }
  }

  public bool HSRFilter
  {
    get => this.m_HSRFilter;
    set
    {
      this.m_HSRFilter = value;
      this.OnPropertyChanged(nameof (HSRFilter));
    }
  }

  public string SelectedColor1
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[0]);
    set
    {
      this.UpdateColor(0, value);
      this.OnPropertyChanged(nameof (SelectedColor1));
    }
  }

  public string SelectedColor2
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[1]);
    set
    {
      this.UpdateColor(1, value);
      this.OnPropertyChanged(nameof (SelectedColor2));
    }
  }

  public string SelectedColor3
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[2]);
    set
    {
      this.UpdateColor(2, value);
      this.OnPropertyChanged(nameof (SelectedColor3));
    }
  }

  public string SelectedColor4
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[3]);
    set
    {
      this.UpdateColor(3, value);
      this.OnPropertyChanged(nameof (SelectedColor4));
    }
  }

  public string SelectedColor5
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[4]);
    set
    {
      this.UpdateColor(4, value);
      this.OnPropertyChanged(nameof (SelectedColor5));
    }
  }

  public string SelectedColor6
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[5]);
    set
    {
      this.UpdateColor(5, value);
      this.OnPropertyChanged(nameof (SelectedColor6));
    }
  }

  public string SelectedColor7
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[6]);
    set
    {
      this.UpdateColor(6, value);
      this.OnPropertyChanged(nameof (SelectedColor7));
    }
  }

  public string SelectedColor8
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[7]);
    set
    {
      this.UpdateColor(7, value);
      this.OnPropertyChanged(nameof (SelectedColor8));
    }
  }

  public string SelectedColor9
  {
    get => ColorTranslator.ToHtml(this.m_selectedColorList[8]);
    set
    {
      this.UpdateColor(8, value);
      this.OnPropertyChanged(nameof (SelectedColor9));
    }
  }

  public List<Color> SelectedColorList
  {
    get => this.m_selectedColorList;
    set => this.m_selectedColorList = value;
  }

  public List<string> ScaleNameList
  {
    get => this.m_scaleNameList;
    set
    {
      this.m_scaleNameList = value;
      this.OnPropertyChanged(nameof (ScaleNameList));
    }
  }

  public List<float> ScaleValueList
  {
    get => this.m_scaleValueList;
    set => this.m_scaleValueList = value;
  }

  public int ScaleValueListSize => this.m_scaleValueList.Count;

  public string SelectedThicknessScaleName
  {
    get => this.m_selectedScaleName;
    set
    {
      this.m_selectedScaleName = value;
      TxApplicationEx.PaintManager().SetThicknessScaleByName(this.m_selectedScaleName);
      this.ScaleValueList = TxApplicationEx.PaintManager().GetCurrentThicknessScaleValues();
      this.OnPropertyChanged(nameof (SelectedThicknessScaleName));
    }
  }

  public bool IsSimulationRunning
  {
    get => this.m_isSimulationRunning;
    set
    {
      this.m_isSimulationRunning = value;
      this.ValidateIsButtonEnabled();
      this.OnPropertyChanged(nameof (IsSimulationRunning));
    }
  }

  public void RegisterSimulationEvents()
  {
    try
    {
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument == null || this.m_registeredForSimulationEvents)
        return;
      this.m_registeredForSimulationEvents = true;
      activeDocument.SimulationPlayer.SimulationCleared += new TxSimulationPlayer_SimulationClearedEventHandler(this.onSimulationCleared);
      activeDocument.SimulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.onSimulationStartedForward);
      activeDocument.SimulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.onSimulationStopped);
      activeDocument.SimulationPlayer.SimulationEndedForward += new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.onSimulationEndedForward);
    }
    catch (Exception ex)
    {
    }
  }

  public void UnRegisterSimulationEvents()
  {
    try
    {
      TxDocument activeDocument = TxApplication.ActiveDocument;
      if (activeDocument == null || !this.m_registeredForSimulationEvents)
        return;
      this.m_registeredForSimulationEvents = false;
      activeDocument.SimulationPlayer.SimulationCleared -= new TxSimulationPlayer_SimulationClearedEventHandler(this.onSimulationCleared);
      activeDocument.SimulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.onSimulationStartedForward);
      activeDocument.SimulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.onSimulationStopped);
      activeDocument.SimulationPlayer.SimulationEndedForward -= new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.onSimulationEndedForward);
    }
    catch (Exception ex)
    {
    }
  }

  private void onSimulationCleared(object sender, TxSimulationPlayer_SimulationClearedEventArgs e)
  {
    this.IsSimulationRunning = false;
  }

  private void onSimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    this.IsSimulationRunning = true;
  }

  private void onSimulationEndedForward(
    object sender,
    TxSimulationPlayer_SimulationEndedForwardEventArgs args)
  {
    this.IsSimulationRunning = false;
  }

  private void onSimulationStopped(
    object sender,
    TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    this.IsSimulationRunning = false;
  }

  private void ValidateIsButtonEnabled()
  {
    this.IsButtonEnabled = this.IsMeshReady && !this.IsSimulationRunning && !this.IsMeshEmphasizing;
  }

  private void UpdateColor(int index, string htmlColorValue)
  {
    Color color = ColorTranslator.FromHtml(htmlColorValue);
    this.m_selectedColorList[index] = color;
    this.UpdateEmphasizeColorOfManager();
  }

  public void UpdateScaleNameList()
  {
    this.ScaleNameList = TxApplicationEx.PaintManager().GetAllThicknessScaleNames();
  }

  private void UpdateColorPaletteSingleton()
  {
    for (int index = 0; index < this.SelectedColorList.Count; ++index)
      TxCommandsColorPaletteSingleton.Instance.SetRecentUsedColor(this.SelectedColorList[index]);
  }

  private void UpdateEmphasizeColorOfManager()
  {
    CApPaintDisplayCoverageManager.Instance.EmphasizeColorList = CUiPaintDisplayCoverageUtil.ConvertToTxColor(this.SelectedColorList);
  }

  private void SetInitialScaleName()
  {
    if (this.ScaleNameList.Count <= 0)
      return;
    string str = CUiPaintDisplayCoverageUtil.LoadThicknessScaleNameFromRegistry();
    if (str == null || !this.ScaleNameList.Contains(str))
    {
      if (this.ScaleNameList.Contains("ColorScale"))
        this.SelectedThicknessScaleName = "ColorScale";
      else
        this.SelectedThicknessScaleName = this.ScaleNameList[0];
    }
    else
      this.SelectedThicknessScaleName = str;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }
}
